package com.tcdp.controllers.view;

import java.io.IOException;

import javax.persistence.NoResultException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.tcdp.daos.PageDao;
import com.tcdp.delegates.PageDelegate;

@Controller
@EnableWebMvc
public class ViewPageController extends AbstractViewController {

	@RequestMapping(value="/{page_url}", method=RequestMethod.GET)
	public ModelAndView viewByUrl(@PathVariable("page_url") String page_url,HttpServletResponse response) throws NoResultException, IOException{
		ModelAndView mv = new ModelAndView("Page");
		
		PageDao pageDao = new PageDao();
		PageDelegate page=null;
		try{
			
			page = new PageDelegate(pageDao.findByUrl(page_url));
			
		}catch(NoResultException e){
			throwError(response, HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		mv.addObject("page",page);
		
		return mv;
	}
	
}
